package com.unionftech.content_login.presenter

import com.unionftech.common.base.mvp.IBaseView
import com.unionftech.common.base.mvp.impl.BasePresenter
import com.unionftech.common.base.mvp.impl.IBaseCallBack
import com.unionftech.common.bean.ResponseResult
import com.unionftech.content_login.bean.request.ForgetPasswordRequest
import com.unionftech.content_login.contract.LoginContract
import com.unionftech.content_login.model.LoginModel

class LoginPresenter : BasePresenter<IBaseView, LoginModel>(),
    LoginContract.ILoginPresenter, LoginContract.IForgotPresenter {

    override fun login(account: String, password: String) {
        getView()?.showLoading()
        val callBack = object : IBaseCallBack<ResponseResult<Any?>> {
            override fun onResponse(response: ResponseResult<Any?>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is LoginContract.ILoginView) {
                        it.handleLoginResult(response)
                    }
                }
            }
        }
        getModel().login(account, password, callBack)
    }

    override fun logout() {
        getView()?.showLoading()
        val callback = object : IBaseCallBack<ResponseResult<Any?>> {
            override fun onResponse(response: ResponseResult<Any?>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is LoginContract.ILogoutView) {
                        it.handleLogout(response)
                    }
                }
            }
        }
        getModel().logout(callback)
    }

    override fun resetPassword(request: ForgetPasswordRequest) {
        getView()?.showLoading()
        val callback = object : IBaseCallBack<ResponseResult<Any?>> {
            override fun onResponse(response: ResponseResult<Any?>) {
                getView()?.hideLoading()
                getView()?.let {
                    if (it is LoginContract.IForgotView) {
                        it.handleResult(response)
                    }
                }
            }
        }
        getModel().resetPassword(request, callback)
    }
}